<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/q3dict.qdoc -->
<head>
  <title>Qt 4.3: Q3Dict Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Q3Dict Class Reference<br /><sup><sup>[<a href="qt3support.html">Qt3Support</a> module]</sup></sup></h1>
<p>The Q3Dict class is a template class that provides a dictionary based on <a href="qstring.html">QString</a> keys. <a href="#details">More...</a></p>
<pre> #include &lt;Q3Dict&gt;</pre><p><b>This class is part of the Qt 3 support library.</b> It is provided to keep old source code working. We strongly advise against using it in new code. See <a href="porting4.html">Porting to Qt 4</a> for more information.</p>
<p>Inherits <a href="q3ptrcollection.html">Q3PtrCollection</a>.</p>
<ul>
<li><a href="q3dict-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-functions"></a>
<h3>Public Functions</h3>
<ul>
<li><div class="fn"/><b><a href="q3dict.html#Q3Dict">Q3Dict</a></b> ( int <i>size</i> = 17, bool <i>caseSensitive</i> = true )</li>
<li><div class="fn"/><b><a href="q3dict.html#Q3Dict-2">Q3Dict</a></b> ( const Q3Dict&lt;type&gt; &amp; <i>dict</i> )</li>
<li><div class="fn"/><b><a href="q3dict.html#dtor.Q3Dict">~Q3Dict</a></b> ()</li>
<li><div class="fn"/>virtual void <b><a href="q3dict.html#clear">clear</a></b> ()</li>
<li><div class="fn"/>virtual uint <b><a href="q3dict.html#count">count</a></b> () const</li>
<li><div class="fn"/>type * <b><a href="q3dict.html#find">find</a></b> ( const QString &amp; <i>key</i> ) const</li>
<li><div class="fn"/>void <b><a href="q3dict.html#insert">insert</a></b> ( const QString &amp; <i>key</i>, const type * <i>item</i> )</li>
<li><div class="fn"/>bool <b><a href="q3dict.html#isEmpty">isEmpty</a></b> () const</li>
<li><div class="fn"/>bool <b><a href="q3dict.html#remove">remove</a></b> ( const QString &amp; <i>key</i> )</li>
<li><div class="fn"/>void <b><a href="q3dict.html#replace">replace</a></b> ( const QString &amp; <i>key</i>, const type * <i>item</i> )</li>
<li><div class="fn"/>void <b><a href="q3dict.html#resize">resize</a></b> ( uint <i>newsize</i> )</li>
<li><div class="fn"/>uint <b><a href="q3dict.html#size">size</a></b> () const</li>
<li><div class="fn"/>void <b><a href="q3dict.html#statistics">statistics</a></b> () const</li>
<li><div class="fn"/>type * <b><a href="q3dict.html#take">take</a></b> ( const QString &amp; <i>key</i> )</li>
<li><div class="fn"/>Q3Dict&lt;type&gt; &amp; <b><a href="q3dict.html#operator-eq">operator=</a></b> ( const Q3Dict&lt;type&gt; &amp; <i>dict</i> )</li>
<li><div class="fn"/>type * <b><a href="q3dict.html#operator-5b-5d">operator[]</a></b> ( const QString &amp; <i>key</i> ) const</li>
</ul>
<ul>
<li><div class="fn"/>4 public functions inherited from <a href="q3ptrcollection.html#public-functions">Q3PtrCollection</a></li>
</ul>
<a name="protected-functions"></a>
<h3>Protected Functions</h3>
<ul>
<li><div class="fn"/>virtual QDataStream &amp; <b><a href="q3dict.html#read">read</a></b> ( QDataStream &amp; <i>s</i>, Q3PtrCollection::Item &amp; <i>item</i> )</li>
<li><div class="fn"/>virtual QDataStream &amp; <b><a href="q3dict.html#write">write</a></b> ( QDataStream &amp; <i>s</i>, Q3PtrCollection::Item <i>item</i> ) const</li>
</ul>
<ul>
<li><div class="fn"/>2 protected functions inherited from <a href="q3ptrcollection.html#protected-functions">Q3PtrCollection</a></li>
</ul>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The Q3Dict class is a template class that provides a dictionary based on <a href="qstring.html">QString</a> keys.</p>
<p>Q3Dict is implemented as a template class. Define a template instance Q3Dict&lt;X&gt; to create a dictionary that operates on pointers to X (X *).</p>
<p>A dictionary is a collection of key-value pairs. The key is a <a href="qstring.html">QString</a> used for insertion, removal and lookup. The value is a pointer. Dictionaries provide very fast insertion and lookup.</p>
<p>If you want to use non-Unicode, plain 8-bit <tt>char*</tt> keys, use the <a href="q3asciidict.html">Q3AsciiDict</a> template. A Q3Dict has the same performance as a <a href="q3asciidict.html">Q3AsciiDict</a>. If you want to have a dictionary that maps QStrings to QStrings use <a href="qmap.html">QMap</a>.</p>
<p>The <a href="q3dict.html#size">size</a>() of the dictionary is very important. In order to get good performance, you should use a suitably large prime number. Suitable means equal to or larger than the maximum expected number of dictionary items. Size is set in the constructor but may be changed with <a href="q3dict.html#resize">resize</a>().</p>
<p>Items are inserted with <a href="q3dict.html#insert">insert</a>(); 0 pointers cannot be inserted. Items are removed with <a href="q3dict.html#remove">remove</a>(). All the items in a dictionary can be removed with <a href="q3dict.html#clear">clear</a>(). The number of items in the dictionary is returned by <a href="q3dict.html#count">count</a>(). If the dictionary contains no items <a href="q3dict.html#isEmpty">isEmpty</a>() returns TRUE. You can change an item's value with <a href="q3dict.html#replace">replace</a>(). Items are looked up with operator[](), or with <a href="q3dict.html#find">find</a>() which return a pointer to the value or 0 if the given key does not exist. You can take an item out of the dictionary with <a href="q3dict.html#take">take</a>().</p>
<p>Calling setAutoDelete(TRUE) for a dictionary tells it to delete items that are removed. The default behavior is not to delete items when they are removed.</p>
<p>When an item is inserted, the key is converted (hashed) to an integer index into an internal hash array. This makes lookup very fast.</p>
<p>Items with equal keys are allowed. When inserting two items with the same key, only the last inserted item will be accessible (last in, first out) until it is removed.</p>
<p>The <a href="q3dictiterator.html">Q3DictIterator</a> class can traverse the dictionary, but only in an arbitrary order. Multiple iterators may independently traverse the same dictionary.</p>
<p>When inserting an item into a dictionary, only the pointer is copied, not the item itself, i.e&#x2e; a shallow copy is made. It is possible to make the dictionary copy all of the item's data (a deep copy) when an item is inserted. <a href="q3dict.html#insert">insert</a>() calls the virtual function <a href="q3ptrcollection.html#newItem">Q3PtrCollection::newItem</a>() for the item to be inserted. Inherit a dictionary and reimplement <a href="q3ptrcollection.html#newItem">newItem</a>() if you want deep copies.</p>
<p>When removing a dictionary item, the virtual function <a href="q3ptrcollection.html#deleteItem">Q3PtrCollection::deleteItem</a>() is called. Q3Dict's default implementation is to delete the item if auto-deletion is enabled.</p>
<p>See also <a href="q3dictiterator.html">Q3DictIterator</a>, <a href="q3asciidict.html">Q3AsciiDict</a>, <a href="q3intdict.html">Q3IntDict</a>, and <a href="q3ptrdict.html">Q3PtrDict</a>.</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="Q3Dict"></a>Q3Dict::Q3Dict ( int <i>size</i> = 17, bool <i>caseSensitive</i> = true )</h3>
<p>Constructs a dictionary optimized for less than <i>size</i> entries.</p>
<p>We recommend setting <i>size</i> to a suitably large prime number (e.g&#x2e; a prime that's slightly larger than the expected number of entries). This makes the hash distribution better which will lead to faster lookup.</p>
<p>If <i>caseSensitive</i> is TRUE (the default), keys which differ only by case are considered different.</p>
<h3 class="fn"><a name="Q3Dict-2"></a>Q3Dict::Q3Dict ( const Q3Dict&lt;type&gt; &amp; <i>dict</i> )</h3>
<p>Constructs a copy of <i>dict</i>.</p>
<p>Each item in <i>dict</i> is inserted into this dictionary. Only the pointers are copied (shallow copy).</p>
<h3 class="fn"><a name="dtor.Q3Dict"></a>Q3Dict::~Q3Dict ()</h3>
<p>Removes all items from the dictionary and destroys it. If <a href="q3ptrcollection.html#setAutoDelete">setAutoDelete</a>() is TRUE, each value is deleted. All iterators that access this dictionary will be reset.</p>
<p>See also <a href="q3ptrcollection.html#setAutoDelete">setAutoDelete</a>().</p>
<h3 class="fn"><a name="clear"></a>void Q3Dict::clear ()&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Removes all items from the dictionary.</p>
<p>The removed items are deleted if <a href="q3ptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.</p>
<p>All dictionary iterators that operate on the dictionary are reset.</p>
<p>Reimplemented from <a href="q3ptrcollection.html#clear">Q3PtrCollection</a>.</p>
<p>See also <a href="q3dict.html#remove">remove</a>(), <a href="q3dict.html#take">take</a>(), and <a href="q3ptrcollection.html#setAutoDelete">setAutoDelete</a>().</p>
<h3 class="fn"><a name="count"></a><a href="qtglobal.html#uint-typedef">uint</a> Q3Dict::count () const&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns the number of items in the dictionary.</p>
<p>Reimplemented from <a href="q3ptrcollection.html#count">Q3PtrCollection</a>.</p>
<p>See also <a href="q3dict.html#isEmpty">isEmpty</a>().</p>
<h3 class="fn"><a name="find"></a>type * Q3Dict::find ( const <a href="qstring.html">QString</a> &amp; <i>key</i> ) const</h3>
<p>Returns the item with key <i>key</i>, or 0 if the key does not exist in the dictionary.</p>
<p>If there are two or more items with equal keys, then the most recently inserted item will be found.</p>
<p>Equivalent to the [] operator.</p>
<p>See also <a href="q3dict.html#operator-5b-5d">operator[]</a>().</p>
<h3 class="fn"><a name="insert"></a>void Q3Dict::insert ( const <a href="qstring.html">QString</a> &amp; <i>key</i>, const type * <i>item</i> )</h3>
<p>Inserts the key <i>key</i> with value <i>item</i> into the dictionary.</p>
<p>Multiple items can have the same key, in which case only the last item will be accessible using <a href="q3dict.html#operator-5b-5d">operator[]</a>().</p>
<p><i>item</i> may not be 0.</p>
<p>See also <a href="q3dict.html#replace">replace</a>().</p>
<h3 class="fn"><a name="isEmpty"></a>bool Q3Dict::isEmpty () const</h3>
<p>Returns TRUE if the dictionary is empty, i.e&#x2e; <a href="q3dict.html#count">count</a>() == 0; otherwise returns FALSE.</p>
<p>See also <a href="q3dict.html#count">count</a>().</p>
<h3 class="fn"><a name="read"></a><a href="qdatastream.html">QDataStream</a> &amp; Q3Dict::read ( <a href="qdatastream.html">QDataStream</a> &amp; <i>s</i>, <a href="q3ptrcollection.html#Item-typedef">Q3PtrCollection::Item</a> &amp; <i>item</i> )&nbsp;&nbsp;<tt> [virtual protected]</tt></h3>
<p>Reads a dictionary item from the stream <i>s</i> and returns a reference to the stream.</p>
<p>The default implementation sets <i>item</i> to 0.</p>
<p>See also <a href="q3dict.html#write">write</a>().</p>
<h3 class="fn"><a name="remove"></a>bool Q3Dict::remove ( const <a href="qstring.html">QString</a> &amp; <i>key</i> )</h3>
<p>Removes the item with <i>key</i> from the dictionary. Returns TRUE if successful, i.e&#x2e; if the item is in the dictionary; otherwise returns FALSE.</p>
<p>If there are two or more items with equal keys, then the last item that was inserted will be removed.</p>
<p>The removed item is deleted if <a href="q3ptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.</p>
<p>All dictionary iterators that refer to the removed item will be set to point to the next item in the dictionary's traversal order.</p>
<p>See also <a href="q3dict.html#take">take</a>(), <a href="q3dict.html#clear">clear</a>(), and <a href="q3ptrcollection.html#setAutoDelete">setAutoDelete</a>().</p>
<h3 class="fn"><a name="replace"></a>void Q3Dict::replace ( const <a href="qstring.html">QString</a> &amp; <i>key</i>, const type * <i>item</i> )</h3>
<p>Replaces the value of the key, <i>key</i> with <i>item</i>.</p>
<p>If the item does not already exist, it will be inserted.</p>
<p><i>item</i> may not be 0.</p>
<p>Equivalent to:</p>
<pre> Q3Dict&lt;char&gt; dict;
     ...
 if ( dict.find( key ) )
     dict.remove( key );
 dict.insert( key, item );</pre>
<p>If there are two or more items with equal keys, then the last item that was inserted will be replaced.</p>
<p>See also <a href="q3dict.html#insert">insert</a>().</p>
<h3 class="fn"><a name="resize"></a>void Q3Dict::resize ( <a href="qtglobal.html#uint-typedef">uint</a> <i>newsize</i> )</h3>
<p>Changes the size of the hash table to <i>newsize</i>. The contents of the dictionary are preserved, but all iterators on the dictionary become invalid.</p>
<h3 class="fn"><a name="size"></a><a href="qtglobal.html#uint-typedef">uint</a> Q3Dict::size () const</h3>
<p>Returns the size of the internal hash array (as specified in the constructor).</p>
<p>See also <a href="q3dict.html#count">count</a>().</p>
<h3 class="fn"><a name="statistics"></a>void Q3Dict::statistics () const</h3>
<p>Debugging-only function that prints out the dictionary distribution using <a href="qtglobal.html#qDebug">qDebug</a>().</p>
<h3 class="fn"><a name="take"></a>type * Q3Dict::take ( const <a href="qstring.html">QString</a> &amp; <i>key</i> )</h3>
<p>Takes the item with <i>key</i> out of the dictionary without deleting it (even if <a href="q3ptrcollection.html#setAutoDelete">auto-deletion</a> is enabled).</p>
<p>If there are two or more items with equal keys, then the last item that was inserted will be taken.</p>
<p>Returns a pointer to the item taken out, or 0 if the key does not exist in the dictionary.</p>
<p>All dictionary iterators that refer to the taken item will be set to point to the next item in the dictionary traversal order.</p>
<p>See also <a href="q3dict.html#remove">remove</a>(), <a href="q3dict.html#clear">clear</a>(), and <a href="q3ptrcollection.html#setAutoDelete">setAutoDelete</a>().</p>
<h3 class="fn"><a name="write"></a><a href="qdatastream.html">QDataStream</a> &amp; Q3Dict::write ( <a href="qdatastream.html">QDataStream</a> &amp; <i>s</i>, <a href="q3ptrcollection.html#Item-typedef">Q3PtrCollection::Item</a> <i>item</i> ) const&nbsp;&nbsp;<tt> [virtual protected]</tt></h3>
<p>Writes a dictionary <i>item</i> to the stream <i>s</i> and returns a reference to the stream.</p>
<p>See also <a href="q3dict.html#read">read</a>().</p>
<h3 class="fn"><a name="operator-eq"></a>Q3Dict&lt;type&gt; &amp; Q3Dict::operator= ( const Q3Dict&lt;type&gt; &amp; <i>dict</i> )</h3>
<p>Assigns <i>dict</i> to this dictionary and returns a reference to this dictionary.</p>
<p>This dictionary is first cleared, then each item in <i>dict</i> is inserted into this dictionary. Only the pointers are copied (shallow copy), unless <a href="q3ptrcollection.html#newItem">newItem</a>() has been reimplemented.</p>
<h3 class="fn"><a name="operator-5b-5d"></a>type * Q3Dict::operator[] ( const <a href="qstring.html">QString</a> &amp; <i>key</i> ) const</h3>
<p>Returns the item with key <i>key</i>, or 0 if the key does not exist in the dictionary.</p>
<p>If there are two or more items with equal keys, then the most recently inserted item will be found.</p>
<p>Equivalent to the <a href="q3dict.html#find">find</a>() function.</p>
<p>See also <a href="q3dict.html#find">find</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
